# pip install redis==3.5.3
# pip install redis-py-cluster==2.1.3

from rediscluster import RedisCluster
from core.config import Config
import redis


class MyRedis(object):

    @classmethod
    def get_connection(cls):

        if hasattr(Config, "redis_single_host"):
            # 单机
            conn = redis.StrictRedis(host=Config.redis_single_host, port=Config.redis_port, db=0, decode_responses=True)
            return conn
        else:
            # 集群
            conn = RedisCluster(startup_nodes=Config.redis_startup_nodes, decode_responses=True,
                                password=Config.redis_password, skip_full_coverage_check=True)
            return conn


    @classmethod
    def get(cls,key):
        conn = cls.get_connection()
        value = conn.get(key)
        return value

    @classmethod
    def setAndExp(cls, key, val, expSec):
        conn = cls.get_connection()
        conn.set(key, val, ex=expSec)

    @classmethod
    def set(cls, key, val):
        conn = cls.get_connection()
        conn.set(key, val)












